<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Finance;

use AuctionCore\CarResource\CarModel;
use AuctionCore\Other\Address;

/**
 * 车主补偿
 * Class DealerRefund
 *
 * @author Xmh
 */
class CarOwnerCompensate
{
    private $CarOwnerCompensate;
    private $CarModel;
    private $Addr;

    public function __construct()
    {
        $this->CarOwnerCompensate = M('CarOwnerCompensate');
        $this->CarModel = new CarModel();
        $this->Addr = new Address();
    }

    /**
     * 获取退车商款的列表
     *
     * @param integer $page
     * @param int     $size
     * @param array   $params
     *
     * @return multitype:unknown string
     */
    public function get_car_owner_compensate_list($page = 1, $size = 10, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 10 : (int)$size;
        $map = array();
        $map['coc.compensate_amount'] = ['gt', 0];
        if ($params['status']) {
            $map['coc.status'] = ['eq', $params['status']];
        }
        if (!empty($params['location_area'])) {
            $map['c.location_area'] = ['eq', $params['location_area']];
        }
        if (!empty($params['keywords'])) {
            $keywords = trim($params['keywords']);
            $where['o.order_no'] = ['LIKE', '%' . $keywords . '%'];
            $where['c.car_no'] = ['LIKE', '%' . $keywords . '%'];
            $where['co.seller_name'] = ['LIKE', '%' . $keywords . '%'];
            $where['co.phone'] = ['LIKE', '%' . $keywords . '%'];
            $where['_logic'] = 'OR';
            $map['_complex'] = $where;
        }
        if (empty($params['location_area']) && $params['bc_city']) {
            $map['c.location_area'] = ['in', $params['bc_city']];
        }
        //要查询的列表
        $cols = "o.order_id,o.order_no,o.status,o.car_id,o.last_time,
                 c.car_id,c.car_no,c.plate_suffix,c.plate_prefix,c.location_area,c.brand_id,c.series_id,c.model_id,
                 co.seller_name as contact_person,co.phone as contact_mobile,
                 coc.id,coc.compensate_time,coc.createtime";
        $prefix = C('DB_PREFIX');
        $order = 'coc.id DESC,o.order_id DESC';
        $total = $this->CarOwnerCompensate->alias('coc')
            ->join($prefix . "order as o ON o.order_id = coc.order_id")
            ->join($prefix . "cars as c ON c.car_id = coc.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id = coc.owner_id")
            ->where($map)->count();
        $list = $this->CarOwnerCompensate->alias('coc')->field($cols)
            ->join($prefix . "order as o ON o.order_id = coc.order_id")
            ->join($prefix . "cars as c ON c.car_id = coc.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id = coc.owner_id")
            ->where($map)->limit($size)->page($page)->order($order)->select();
//        echo M()->_sql();

        if (!empty($list)) {
            foreach ($list as $key => $val) {
                $list[$key]['order_no'] = $val['order_no'];
                $list[$key]['location_area_name'] = $this->Addr->get_name_by_code($val['location_area']);
                $brand = $this->CarModel->get_model_by_id($val['brand_id'], 'brand');
                $series = $this->CarModel->get_model_by_id($val['series_id'], 'series');
                $model = $this->CarModel->get_model_by_id($val['model_id']);
                $list[$key]['car_no'] = $val['car_no'];
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
                $list[$key]['createtime'] = date("Y-m-d H:i", strtotime($val['createtime']));
                $list[$key]['compensate_time'] = date("Y-m-d H:i", strtotime($val['compensate_time']));
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 退车商款详情
     *
     * @param  int $com_id
     *
     * @return mixed
     */
    public function car_owner_compensate_detail($com_id)
    {
        $condition['coc.id'] = ['eq', $com_id];
        $condition['bc.status'] = ['eq', 2];
        //要查询的列表
        $cols = "o.order_id,o.order_no,o.status,o.car_id,o.last_time,
                 c.car_id,c.car_no,c.plate_suffix,c.plate_prefix,c.location_area,c.brand_id,c.series_id,c.model_id,
                 c.car_color,c.emission,c.pointer_mileage,c.reg_city,c.first_reg_date,c.composite_state,c.history_state,
                 c.accident_level,c.transfer_limit,c.vin,c.car_source,
                 co.seller_name as contact_person,co.phone as contact_mobile,co.owner_type,co.contact_person as company_contact_person,
                 coc.id,coc.remark as compensate_remark,coc.compensate_time,coc.emp_id as compensate_emp,
                 coc.compensate_amount,coc.status as compensate_status,
                 bc.emp_id as bc_emp_id,bc.compensate_type,bc.compensate,bc.remark as bc_remark,bc.deal_time,bc.bank_account";
        $prefix = C('DB_PREFIX');
        $order = 'o.order_id DESC';
        $res = $this->CarOwnerCompensate->alias('coc')->field($cols)
            ->join($prefix . "order as o ON o.order_id = coc.order_id")
            ->join($prefix . "cars as c ON c.car_id = coc.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id = coc.owner_id")
            ->join($prefix . "breach_car as bc ON bc.car_id = coc.car_id")
            ->where($condition)->order($order)->find();
//        echo M()->_sql();
        $brand = $this->CarModel->get_model_by_id($res['brand_id'], 'brand');
        $series = $this->CarModel->get_model_by_id($res['series_id'], 'series');
        $model = $this->CarModel->get_model_by_id($res['model_id']);
        $res['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
        $res['car_color'] = C('CAR_COLOR.' . $res['car_color']);
        $res['emission'] = C('INT_EMISSION_STD.' . $res['emission']);
        $res['plate_prefix'] = C('PLATE_PREFIX.' . $res['plate_prefix']);
        $res['pointer_mileage'] = round($res['pointer_mileage'] / 10000, 2);
        $res['location_area_name'] = $this->Addr->get_name_by_code($res['location_area']);
        $res['reg_city'] = $this->Addr->get_name_by_code($res['reg_city']);
        $res['car_plate_number'] = C('PLATE_PREFIX')[$res['plate_prefix']] . $res['plate_suffix'];
        //补偿信息
        if ($res['compensate_emp'] > 0) {
            $employee = \AuctionCore\Account\Back\Employee::user_info_id($res['compensate_emp']);
            $res['compensate_emp_name'] = $employee['real_name'];
            $res['compensate_time'] = date("Y年m月d日 H:i", strtotime($res['compensate_time']));
        }
        //打款帐号
        if ($res['car_source'] == 1) {
            $bank_account = json_decode($res['bank_account'], true);
            $res['bank_name'] = $bank_account['bank_name'];
            $res['card_no'] = $bank_account['card_no'];
            $res['account_name'] = $bank_account['account_name'];
        }
        $res['owner_type_name'] = C('OWNER_TYPE_NAME')[$res['owner_type']];
        return $res;
    }

    /**
     * 创建车主补偿
     *
     * @param array $data
     */
    public function add_car_owner_compensate($data = array())
    {
        if (empty($data)) {
            return false;
        }
        return $this->CarOwnerCompensate->add($data);
    }


    /**
     * 更新车主补偿处理
     *
     * @param array $data
     */
    public function save_car_owner_compensate($data = array())
    {
        if (empty($data)) {
            return false;
        }
        $data['compensate_time'] = date("Y-m-d H:i:s", time());
        return $this->CarOwnerCompensate->save($data);
    }

    /**
     * 获取车主补偿详情
     *
     * @param $id
     * @param $cols
     *
     * @return bool|mixed
     */
    public function get_car_owner_compensate($id, $cols = "*")
    {
        if ($id < 1) {
            return false;
        }
        return $this->CarOwnerCompensate->field($cols)->find($id);
    }

}